package kuba.daos;

import java.util.Collection;

import kuba.bean.Author;
import kuba.bean.CD;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;


public class CdSqlDAO implements CdDAO{
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public Collection<CD> getAll(){
        return jdbcTemplate.query("SELECT * FROM cds ", new BeanPropertyRowMapper<CD>( CD.class)) ;
    }
    
    public Collection<CD> getByAuthor(Author author){
        return jdbcTemplate.query("SELECT * FROM cds where author_id =?" ,
                new Object[] {author.getId()},new BeanPropertyRowMapper<CD>(CD.class));
    }


    @Override
    public void insert(CD cd,Author author) {
        String sql = "INSERT INTO CDS VALUES (?,?,?,?)";
        jdbcTemplate.update(sql, new Object [] {0,cd.getTitle(),author.getId(),cd.getGenere()});
    }


    @Override
    public void update(CD cd,Author author) {
        String sql = "UPDATE CDS SET TITLE=?, GENERE=?,AUTHOR_ID =? WHERE ID =? ";
        jdbcTemplate.update(sql, new Object [] {cd.getTitle(),cd.getGenere(),author.getId(),cd.getId()});
        
    }


    @Override
    public void delete(CD cd) {
        String sql = "DELETE FROM CDS WHERE ID = ?";
        jdbcTemplate.update(sql, new Object [] {cd.getId()});
    }
}
